package edu.calpoly.csc.coursebook.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import edu.calpoly.csc.coursebook.business.beans.FinderBean;
import edu.calpoly.csc.coursebook.business.model.CourseManager;

public class CourseBookController extends SimpleFormController
{
   /** Logger for this class and subclasses */
   protected final Log log = LogFactory.getLog(this.getClass());
   
   private CourseManager courseMan;
   
   public ModelAndView onSubmit(Object command) throws ServletException
   {
      FinderBean find = (FinderBean) command;
      log.info("CoursebookController - returning courses view");
      
      String now = (new java.util.Date()).toString();
      log.info("returning courses view with " + now);
      
      Map<String, Object> myModel = new HashMap<String, Object>();
      myModel.put("now", now);
      
      myModel.put("courses", this.getCourseManager().findCourses(find.getInterests()));
      myModel.put("keywords", this.getCourseManager().getKeywords());
      
      //myModel.put("courses", this.getCourseManager().getCourses());
      
      /* first parameter - forwards to courses.jsp in WEB-INF/jsp/ */
      return new ModelAndView("courses", "model", myModel);
   }
   
   public void setCourseManager(CourseManager cm)
   {
      this.courseMan = cm;
   }
   
   public CourseManager getCourseManager()
   {
      return this.courseMan;
   }
   
}
